﻿using BITUI.Tile;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.Element
{
    public class Image : IElement2D_Resizeable
    {
        public Image(Tile.RenderTile tile, TileRenderScale scale = TileRenderScale.One)
        {
            this.tile = tile;
            this.scale = scale;
            this._tileWidth = 1;
            this._tileHeight = 1;
        }
        public Tile.RenderTile tile;
        public TileRenderScale scale;
        public bool FillMode;//In fillMode, auto scale tile to Fill
        public IElement2DGroup Parent
        {
            get;
            private set;
        }
        void IElement2D.SetParent(IElement2DGroup parent)
        {
            this.Parent = parent;
        }

        public int TileXLocal
        {
            get;
            set;
        }

        public int TileYLocal
        {
            get;
            set;
        }
        public int TileX
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileX + this.TileXLocal;
                else
                    return this.TileXLocal;
            }
        }

        public int TileY
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileY + this.TileYLocal;
                else
                    return this.TileYLocal;
            }
        }

        int _tileWidth;
        int _tileHeight;
        public int TileWidth
        {
            get
            {
                return FillMode ? _tileWidth : 1;
            }
            set
            {
                _tileWidth = value;
            }
        }

        public int TileHeight
        {
            get
            {
                return FillMode ? _tileHeight : 1;
            }
            set
            {
                _tileHeight = value;
            }
        }

        public void OnDraw(IWindowRender render)
        {
            if (FillMode)
            {
                var sx = this.TileWidth * 8 / tile.srcrect.w;
                var sy = this.TileHeight * 8 / tile.srcrect.h;
                render.DrawTile(tile, TileX * 8, TileY * 8, 0, sx, sy);

            }
            else
            {
                var _s = scale.ToScaleValue();
                render.DrawTile(tile, TileX * 8, TileY * 8, 0, _s, _s);
            }
        }

    }
}
